package com.imooc.sell.dao;

import com.imooc.sell.dataobject.SellByDay;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 用于日数据统计
 */
public interface DayStatDao extends JpaRepository<SellByDay, Integer> {

    /**
     * 统计昨日销量
     */
    @Query(value = " select count(o.order_id) from order_master o where o.pay_status=1 AND o.create_time BETWEEN CURDATE()-1 AND CURDATE()", nativeQuery = true)
    Integer countYesSuccess();

    /**
     * 统计昨日销售额
     */
    @Query(value = "select sum(o.order_amount)as success_yes_sale from order_master o where o.pay_status=1 AND create_time BETWEEN CURDATE() AND CURDATE()+1", nativeQuery = true)
    BigDecimal countYesSuccessSale();

    /**
     * 统计昨日订单取消数
     */
    @Query(value = "select count(o.order_id) from order_master o where o.order_status=2 AND o.create_time BETWEEN CURDATE()-1 AND CURDATE()", nativeQuery = true)
    Integer countYesCancel();

    /**
     * 统计昨日订单退款数
     */
    @Query(value = "select count(o.order_id) from order_master o where o.pay_status=2 AND o.create_time BETWEEN CURDATE()-1 AND CURDATE()", nativeQuery = true)
    Integer countYesPayback();

    /**
     * 统计最新的两条日统计数据
     */
    @Query(value = "SELECT  * FROM sell_by_day ORDER BY update_time DESC limit 0,2", nativeQuery = true)
    List<SellByDay> findLatest2Records();

    /**
     * 按时间范围查询并分页
     */
    Page<SellByDay> findByUpdateTimeBetween(Date date1, Date date2, Pageable pageable);
}
